<?php
//订单管理
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
cfc('core')->checklogin();
if($_RQ['op']=='list'){
	$basic=$this->plugin_setting('basic');
	
	$return=array();
	//查询用户所有挪车卡订单
	$fields=array('id','uniacid','mid','ordersn','status','pay_type','pay_id','createtime','price');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$params = array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$return['params']=array();
	if(isset($_RQ['status'])){
		$params['status'] = intval($_RQ['status']);
		$return['params']['status']=$params['status'];
	}
	$order_list=pdo_getslice('jy_move_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($order_list as &$item){
		$item['createtime']=date("Y/m/d H:i:s",$item['createtime']);
		//$item['price']=$basic['express_fee'];
	}
	$status=array('0'=>'待支付','1'=>'待发货','2'=>'待收货','3'=>'已完成');
	$return['list']=$order_list;
	$return['status']=$status;
	exi($return);
	
}
if($_RQ['op']=='add'){
	$return=array();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$setting=$this->plugin_setting('basic');
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('jy_move_order','ordersn'),
			'status'=>'0',
			'price'=>intval($setting['express_fee']),
			'ship_name'=>trim($postdata['ship_name']),
			'ship_mobile'=>trim($postdata['ship_mobile']),
			'ship_address'=>trim($postdata['ship_address']),
			'createtime'=>TIMESTAMP,
		);	
		pdo_insert('jy_move_order',$order);
		$id=pdo_insertid();
		//获取设置的快递费用
		$params['fee']=$order['price'];
		$params['title']='挪车卡订单';
		$params['id']=$id;
		$params['plugin']='jy_move';
		$params['back_url']='jy_move/order/payresult';
		cfc('core')->pay($params);
	}
	//检查是否已领取挪车卡
	$setting=$this->plugin_setting('basic');
	if($setting['only_one']){
		$check=pdo_getcolumn('jy_move_order',array('mid'=>$_SESSION['uid']),'id');
		if($check){
			exi('您已申请挪车卡，无需重复申请！','error');
		}
	}
	
	$return['item']=array();
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_move_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_move_order',array('pay_type'=>$payresult['pay_type'],'status'=>1,'pay_id'=>$payresult['pay_id']),array('id'=>$order['id']));
	exi('支付成功','success','jy_move/order/list');
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	//订单信息
	$order=pdo_get('jy_move_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return=array();
	$return['item']=$order;
	exi($return);
}
//收货
if($_RQ['op']=='receive'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要收货的订单！','error');
	}
	$this->update('order',array('status'=>'3'),array('id'=>$id));
	exi('收货成功！','','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	pdo_delete('jy_move_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	
	exi('删除成功！','','list');
}
if($_RQ['op']=='pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$order=pdo_get('jy_move_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$params['fee']=$order['price'];
	$params['title']='挪车卡订单';
	$params['id']=$id;
	$params['plugin']='jy_move';
	$params['back_url']='jy_move/order/payresult';
	cfc('core')->pay($params);
}
?>